import random


from configs import errorCode, config
from views import BaseHandler
from models import execute


class IndexAdvertisement(BaseHandler):

    async def get(self, *args, **kwargs):
        """
        ---
        tags:
          - 首页
        summary: 广告轮播图
        description:
        parameters:
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        try:
            _, res = await execute('select img from good where id < %s;', (8, ))
        except Exception as e:
            self.logger.error(e)
            return self.write_error(errorCode.DB_ERR, '数据请求错误,请重新请求')
        img = []
        for i in res:
            i['img'] = config.IMG_PATH + i['img']
            img.append(i["img"])
        return self.write_json(data=img)


class IndexBanner(BaseHandler):

    async def get(self, *args, **kwargs):
        """
        ---
        tags:
          - 首页
        summary: 横幅图
        description:
        parameters:
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        try:
            _, res = await execute('select img from good where id < %s;', (9, ))
        except Exception as e:
            self.logger.error(e)
            return self.write_error(errorCode.DB_ERR, '数据请求错误,请重新请求')
        img = []
        for i in res:
            i['img'] = config.IMG_PATH + i['img']
            img.append(i["img"])
        return self.write_json(data=img)


class IndexShowGood(BaseHandler):

    async def get(self, *args, **kwargs):
        """
        ---
        tags:
          - 首页
        summary: 推荐/精品/热销
        description: 三个功能使用了同一个接口业务处理
        parameters:
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        _, res = await execute('select id, title, img, price from good where status = %s;', (1, ))
        for i in res:
            temp = i['price']
            i['price'] = float(temp)
            i['img'] = config.IMG_PATH + i['img']
        random.shuffle(res)
        return self.write_json(data=res)


class HomeCategoryIcon(BaseHandler):

    async def get(self):
        """
        ---
        tags:
          - 首页
        summary: 一级分类图标
        description: 取了前四个一级分类
        parameters:
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        _, goods_categorys = await execute('select id, title, img from good_cat where pid = 0 limit 4')
        for goods_category in goods_categorys:
            goods_category['img'] = config.IMG_PATH + goods_category['img']
        self.write_json(data=goods_categorys)

